<?php
namespace zake\helper;

use zake\foundation\able\MapAble;


/**
 * Class Map
 * @package zake\helper
 */
class ArrayMap implements MapAble
{
    /**
     * 元素载体
     * @var array
     */
    private $carrier = [];


    /**
     * 列表
     *
     * @return array
     *
     * @author liu.bin 2020-03-24 22:19
     */
    public function all()
    {
        return $this->carrier;
    }


    /**
     * 获取元素
     * @param mixed $key
     * @param null|mixed $default
     * @return mixed
     *
     * @author liu.bin 2020-03-31 00:18
     */
    public function get($key,$default=null)
    {
        return $this->carrier[$key]??$default;
    }


    /**
     * 添加元素映射
     * @param mixed $key
     * @param mixed $node
     *
     * @return bool
     * @author liu.bin 2020-03-24 22:08
     */
    public function put($key,$node)
    {
        $this->carrier[$key] = $node;
        return true;
    }


    /**
     * 是否包含key
     *
     * @param mixed $key
     * @return bool
     * @author liu.bin 2020-03-24 22:08
     */
    public function containerKey($key)
    {
        return isset($this->carrier[$key]) ? true : false;
    }


    /**
     * 是否包含value
     * @param mixed $node
     *
     * @return bool
     * @author liu.bin 2020-03-31 00:19
     */
    public function containerValue($node)
    {
        return in_array($node,$this->carrier) ? true : false;
    }


    /**
     * 移除
     * @param mixed $key
     * @return bool
     * @author liu.bin 2020-03-24 22:09
     */
    public function remove($key)
    {
        if($this->containerKey($key)){
            $this->carrier[$key] = null;
            unset($this->carrier[$key]);
        }
        return true;
    }






    /**
     * 映射数量
     * @return int
     *
     * @author liu.bin 2020-03-30 19:06
     */
    public function size()
    {
        return count($this->carrier);
    }


    /**
     * 是否为空
     * @return bool
     *
     * @author liu.bin 2020-03-30 19:06
     */
    public function isEmpty()
    {
        return 0 === $this->size();
    }


    /**
     * 清空map数据
     * @return bool
     *
     * @author liu.bin 2020-03-30 19:09
     */
    public function clear()
    {
        $this->carrier = [];
        return true;
    }

}